package view.component.monster.small_monster
{
	import flash.text.TextFormat;
	
	import enums.main.UISize;
	import enums.monster.SmallMonsterTypeNavigatorScreenName;
	
	import feathers.controls.Radio;
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.core.ToggleGroup;
	
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.AtlasTextureGenerator;
	import util.FG2FUGenerator;
	
	import view.transition.AnimationNaviTransitionManager;
	
	
	/**
	 * 小型怪物情报界面
	 * @author £iang
	 * 
	 */	
	public class SmallMonsterView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 小型怪物分类切换按钮组 */
		public var buttonVec:Vector.<Radio>;
		/** 小型怪物分类列表导航栏 */
		public var typeListNavigator:ScreenNavigator;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** tabBar列数 */
		private var _tabColumnNum:int;
		/** tabBar间隔 */
		private var _tabGap:Number;
		/** 单选组 */
		private var _toggleGroup:ToggleGroup;
		/** 转场管理 */
		private var _transitManager:AnimationNaviTransitionManager;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var labelVec:Vector.<String> = new Vector.<String>;
			
			labelVec.push( 
				"兽龙种", "草食种", "鸟龙种", "甲虫种", 
				"牙兽种", "鱼龙种", "蛇龙种", "两生种"
			);
			
			buttonVec = new Vector.<Radio>();
			_toggleGroup = new ToggleGroup();
			
			var len:int = labelVec.length;
			var tmpButton:Radio;
			var iconVec:Vector.<Texture> = AtlasTextureGenerator.getRandomCatTextures(
				"head", len );
			var tmpImage:Image;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				tmpButton = FG2FUGenerator.generateRoundRectRadio(
					UISize.LMONSTER_LIST_TBUTTON_W, UISize.TAB_BUTTON_H, 
					this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
				);
				tmpButton.label = labelVec[ i ];
				tmpImage = new Image( iconVec[ i ] );
				tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
				tmpButton.defaultSelectedIcon = tmpImage;
				tmpButton.selectedHoverLabelProperties.textFormat = new TextFormat();
				tmpButton.gap = -50 * this.dpiScale;
				tmpButton.iconPosition = "right";
				tmpButton.toggleGroup = _toggleGroup;
				buttonVec.push( tmpButton );
				this.addChild( buttonVec[ i ] );
			}
			
			this.typeListNavigator = new ScreenNavigator();
			this.addChild( typeListNavigator );
			
			this.typeListNavigator.addScreen(
				SmallMonsterTypeNavigatorScreenName.NONE,
				new ScreenNavigatorItem( Screen )
			);
			
			this.typeListNavigator.addScreen(
				SmallMonsterTypeNavigatorScreenName.ORC_SCREEN,
				new ScreenNavigatorItem( SMOrcTypeView )
			);
			
			this.typeListNavigator.addScreen(
				SmallMonsterTypeNavigatorScreenName.HERB_SCREEN,
				new ScreenNavigatorItem( SMHerbivoreView )
			);
			
			this._transitManager = new AnimationNaviTransitionManager( typeListNavigator );
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			var minColumnNum:int = int(
				( UISize.MAIN_W / this.dpiScale - UISize.TAB_BUTTON_INDENT * 2 + 20 ) /
				( UISize.LMONSTER_LIST_TBUTTON_W + 20 )
			);
			var maxColumnNum:int = minColumnNum + 1;
			var minTabGap:Number = 
				( UISize.MAIN_W / this.dpiScale - UISize.TAB_BUTTON_INDENT * 2 - maxColumnNum * UISize.LMONSTER_LIST_TBUTTON_W ) / 
				minColumnNum;
			var totalNum:Number = buttonVec.length;
			
			if ( minTabGap >= 20 )
			{
				_tabColumnNum = maxColumnNum;
				_tabGap = minTabGap;
			}
			else
			{
				_tabColumnNum = minColumnNum;
				_tabGap = ( UISize.MAIN_W / this.dpiScale - UISize.TAB_BUTTON_INDENT * 2 - minColumnNum * UISize.LMONSTER_LIST_TBUTTON_W ) / 
					( minColumnNum - 1 );
			}
			
			var tmpButton:Radio;
			var columnPos:int;
			var rowPos:int;
			
			for ( var i:int = 0; i < totalNum; i ++ )
			{
				tmpButton = buttonVec[ i ] as Radio;
				columnPos = ( i % _tabColumnNum );
				rowPos = int( i / _tabColumnNum );
				tmpButton.x = ( UISize.TAB_BUTTON_INDENT + 
					columnPos * ( UISize.LMONSTER_LIST_TBUTTON_W + _tabGap ) ) * this.dpiScale;
				tmpButton.y = UISize.MAIN_H - ( UISize.LMONSTER_LIST_TAB_AREA_H + UISize.MONSTER_TAB_AREA_H - 20 ) * this.dpiScale +
					( UISize.TAB_BUTTON_H + 20 ) * rowPos * this.dpiScale;
			}
			
			showList();
		}
		
		/**
		 * 创建时显示第一页列表 
		 * 
		 */		
		public function showList():void
		{
			typeListNavigator.showScreen( SmallMonsterTypeNavigatorScreenName.ORC_SCREEN );
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			this._toggleGroup.addEventListener( Event.CHANGE, toggleChangeHandler );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理单选改变时派发的事件 
		 * @param event 单选改变时派发的事件 
		 * 
		 */		
		protected function toggleChangeHandler( event:Event ):void
		{
			var index:int = _toggleGroup.selectedIndex;
			
			switch(index)
			{
				case 0:
				{
					typeListNavigator.showScreen( SmallMonsterTypeNavigatorScreenName.ORC_SCREEN );
					break;
				}
					
				case 1:
				{
					typeListNavigator.showScreen( SmallMonsterTypeNavigatorScreenName.HERB_SCREEN );
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		
		public function clearNavi():void
		{
			//typeListNavigator.showScreen( SmallMonsterTypeNavigatorScreenName.NONE );
			_transitManager.clear();
			typeListNavigator.removeAllScreens();
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			this._toggleGroup.removeEventListeners();
			
			var len:int = buttonVec.length;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				( buttonVec[ i ] as Radio ).dispose();
				FG2FUGenerator.deepDisposeButtonComp( buttonVec[ i ] );
				buttonVec[ i ] = null;
			}
			
			buttonVec.length = 0;
			buttonVec = null;
			_toggleGroup = null;
			typeListNavigator.dispose();
			typeListNavigator = null;
			_transitManager = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function SmallMonsterView()
		{
			super();
		}
	}
}